/**
 *
 */
package de.cbf.cam.dsa.rcp.model.persistence.db4o.extension;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
//
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.preference.IPreferenceStore;

import de.cbf.cam.dsa.rcp.model.entities.CultureDSA;
import de.cbf.cam.dsa.rcp.model.entities.ProfessionDSA;
import de.cbf.cam.dsa.rcp.model.entities.RaceDSA;
import de.cbf.cam.dsa.rcp.model.entities.StructureData;
import de.cbf.cam.dsa.rcp.model.persistence.db4o.RCPEntityPersisterDB4O;
import de.cbf.cam.dsa.rcp.model.persistence.db4o.act.ActivatorRCPModelPersistenceDb4o;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.AbstractParameteredPersister;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.IRCPPersisterCulture;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.IRCPPersisterProfession;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.IRCPPersisterRace;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.IRCPPersisterStructure;
import de.cbf.util.db.interfaces.exceptions.EntityLoadException;
import de.cbf.util.db.interfaces.exceptions.EntitySaveException;
import de.cbf.util.uuidheader.interfaces.IUUIDHeader;

/**
 * @author salter
 *
 */
public class PersisterExtensionGatewayDB4O extends AbstractParameteredPersister implements
	IRCPPersisterRace,IRCPPersisterCulture,IRCPPersisterProfession,IRCPPersisterStructure{
//	private static final Log logger = LogFactory.getLog(PersisterExtensionGatewayDB4O.class);

	public static final String NAME = "RCP DB4O Persister";

	protected RCPEntityPersisterDB4O persister;

	protected IPreferenceStore store;

	/**
	 *
	 */
	public PersisterExtensionGatewayDB4O() {
		setName(NAME);
		store = ActivatorRCPModelPersistenceDb4o.getDefault().getPreferenceStore();
		persister = new RCPEntityPersisterDB4O();
	}

	protected String importParametersOK() {

		return "";
	}
	protected String exportParametersOK() {
		return "";
	}

	@Override
	public Collection<Object> getKnownLoadParameters() {
		List<Object> parameters = new ArrayList<Object>();
		return parameters;
	}

	@Override
	public Collection<Object> getKnownSaveParameters() {
		List<Object> parameters = new ArrayList<Object>();
		return parameters;
	}

	public void saveEntity(IUUIDHeader entity) throws EntitySaveException{
		persister.saveEntity(entity);
	}

	@Override
	public RaceDSA loadRace(UUID uuid) throws EntityLoadException {
		//check if file is specified
//		IUUIDHeader entity = persister.loadEntity(uuid);
//		if (entity instanceof RaceDSA) {
//			RaceDSA race = (RaceDSA) entity;
//			return race;
//		}
//		throw new EntityLoadException("no race found for uuid "+uuid);
		Collection<IUUIDHeader> entities = persister.loadEntities(RaceDSA.class);
		for (IUUIDHeader entity : entities) {
			if (entity instanceof RaceDSA) {
				RaceDSA race = (RaceDSA) entity;
				if(entity.getUuid().toString().equals(uuid.toString())){
					return race;
				}
			}
		}
		throw new EntityLoadException("no race found for uuid "+uuid);
	}

	@Override
	public Collection<RaceDSA> loadAllRaces()throws EntityLoadException {
		Collection<RaceDSA> result = new ArrayList<RaceDSA>();
		Collection<IUUIDHeader> loadedEntities = persister.loadEntities(RaceDSA.class);
		for (IUUIDHeader iuuidHeader : loadedEntities) {
			if (iuuidHeader instanceof RaceDSA) {
				RaceDSA race = (RaceDSA) iuuidHeader;
				result.add(race);
			}
		}
		return result;
	}

	@Override
	public void saveRace(RaceDSA entity) throws EntitySaveException {
		saveEntity(entity);
	}

	@Override
	public CultureDSA loadCulture(UUID uuid) throws EntityLoadException {
//		IUUIDHeader entity = persister.loadEntity(uuid);
//		if (entity instanceof CultureDSA) {
//			CultureDSA culture = (CultureDSA) entity;
//			return culture;
//		}
//		throw new EntityLoadException("no culture found for uuid "+uuid);
		Collection<IUUIDHeader> entities = persister.loadEntities(CultureDSA.class);
		for (IUUIDHeader entity : entities) {
			if (entity instanceof CultureDSA) {
				CultureDSA culture = (CultureDSA) entity;
				if(entity.getUuid().toString().equals(uuid.toString())){
					return culture;
				}
			}
		}
		throw new EntityLoadException("no culture found for uuid "+uuid);
	}

	@Override
	public Collection<CultureDSA> loadAllCultures() throws EntityLoadException {
		Collection<CultureDSA> result = new ArrayList<CultureDSA>();
		Collection<IUUIDHeader> loadedEntities = persister.loadEntities(CultureDSA.class);
		for (IUUIDHeader iuuidHeader : loadedEntities) {
			if (iuuidHeader instanceof CultureDSA) {
				CultureDSA culture = (CultureDSA) iuuidHeader;
				result.add(culture);
			}
		}
		return result;
	}

	@Override
	public void saveCulture(CultureDSA entity) throws EntitySaveException {
		saveEntity(entity);
	}



	@Override
	public ProfessionDSA loadProfession(UUID uuid) throws EntityLoadException {
//		IUUIDHeader entity = persister.loadEntity(uuid);
//		if (entity instanceof ProfessionDSA) {
//			ProfessionDSA profession = (ProfessionDSA) entity;
//			return profession;
//		}
		Collection<IUUIDHeader> entities = persister.loadEntities(ProfessionDSA.class);
		for (IUUIDHeader entity : entities) {
			if (entity instanceof ProfessionDSA) {
				ProfessionDSA profession = (ProfessionDSA) entity;
				if(entity.getUuid().toString().equals(uuid.toString())){
					return profession;
				}
			}
		}
		throw new EntityLoadException("no profession found for uuid "+uuid);
	}

	@Override
	public Collection<ProfessionDSA> loadAllProfessions() throws EntityLoadException {
		Collection<ProfessionDSA> result = new ArrayList<ProfessionDSA>();
		Collection<IUUIDHeader> loadedEntities = persister.loadEntities(ProfessionDSA.class);
		for (IUUIDHeader iuuidHeader : loadedEntities) {
			if (iuuidHeader instanceof ProfessionDSA) {
				ProfessionDSA profession = (ProfessionDSA) iuuidHeader;
				result.add(profession);
			}
		}
		return result;
	}

	@Override
	public void saveProfession(ProfessionDSA entity) throws EntitySaveException {
		saveEntity(entity);
	}

	@Override
	public StructureData loadStructure(UUID uuid) throws EntityLoadException {
//		IUUIDHeader entity = persister.loadEntity(uuid);//TODO while simple loading does not work
//		if (entity instanceof StructureData) {
//			StructureData structureDataCached = (StructureData) entity;
//			return structureDataCached;
//		}
//		throw new EntityLoadException("no structure data found for uuid "+uuid);
		Collection<IUUIDHeader> entities = persister.loadEntities(StructureData.class);
		for (IUUIDHeader entity : entities) {
			if (entity instanceof StructureData) {
				StructureData structureData = (StructureData) entity;
//				if(entity.getUuid().toString().equals(uuid.toString())){
//					return structureData;
//				}
				return structureData;
			}
		}
		throw new EntityLoadException("no structure data found for uuid "+uuid);
	}

	public Collection<StructureData> loadAllStructureData() throws EntityLoadException {
		Collection<StructureData> result = new ArrayList<StructureData>();
		Collection<IUUIDHeader> loadedEntities = persister.loadEntities(StructureData.class);
		for (IUUIDHeader iuuidHeader : loadedEntities) {
			if (iuuidHeader instanceof StructureData) {
				StructureData structureData = (StructureData) iuuidHeader;
				result.add(structureData);
			}
		}
		return result;
	}

	@Override
	public void saveStructure(StructureData entity) throws EntitySaveException {
		saveEntity(entity);
	}

	@Override
	public void tidyUpStructures(List<UUID> uuids) {
		//no need to tidy up anything
	}

	@Override
	public void tidyUpProfessions(List<UUID> uuids) {
		//no need to tidy up anything
	}

	@Override
	public void tidyUpCultures(List<UUID> uuids) {
		//no need to tidy up anything
	}

	@Override
	public void tidyUpRaces(List<UUID> uuids) {
		//no need to tidy up anything
	}







}
